<?php

class notifications extends post{
    
    //public function testNoti(){
    //    $auth = $this->getAuth('current_user');
    //    $ret = "";
    //    $noti_array = array();
    //    $noti_name = array();
    //    $noti_get_tbl = $this->query("SELECT *
    //                                FROM tbnotification
    //                                WHERE userID={$this->escape($auth['id'])}
    //                                AND is_active={$this->escape(1)}
    //                                ORDER BY date DESC","array");
    //    
    //        
    //    foreach($noti_get_tbl as $noti){
    //        array_push($noti_name,$noti_get_tbl);
    //        
    //        
    //        // For Mention
    //        if($noti['type']=="1"){ // Mention Post
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //        }elseif($noti['type']=="6"){ // Mention Reply
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy';
    //            $left_select = ' l.id=n.noti_id';
    //        }
    //        
    //        // For Like
    //        elseif($noti['type']=="3"){
    //            $define = ',tp.id as nID,tp.postedBy as postBy,l.likeType as lType';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.user_id  LEFT JOIN tbpost tp on tp.id=l.post_id';
    //            $left_select = ' l.post_id=n.noti_id';
    //        }
    //        elseif($noti['type']=="4"){
    //            $define = ',tp.id as nID,tp.postedBy as postBy,l.likeType as lType';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.user_id  LEFT JOIN tbpost tp on tp.id=l.post_id';
    //            $left_select = ' l.post_id=n.noti_id';
    //        }
    //        
    //        // For Comment
    //        elseif($noti['type']=="2"){
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy';
    //            $left_select = ' l.id=n.noti_id';
    //        }
    //        
    //        // 
    //        // For Task
    //        elseif($noti['type']=="10"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }elseif($noti['type']=="11"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //           
    //        }elseif($noti['type']=="12"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }elseif($noti['type']=="13"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }
    //        
    //        $getNoti .= "SELECT 
    //                                n.*,
    //                                n.id as notiID,
    //                                u.id as uID,
    //                                us.first_name as uFname,
    //                                us.last_name as uLname,
    //                                n.date as notiDate,
    //                                u.first_name as firstname,
    //                                u.last_name as lastname
    //                                " . $define . "
    //                                FROM tbnotification n
    //                                LEFT JOIN tbuser u on u.id=n.noti_by
    //                                LEFT JOIN " . $noti['table_name'] . " l on " . $left_select . "
    //                                " .  $left_join . "
    //                                WHERE n.userID={$this->escape($auth['id'])}
    //                                AND u.company_id={$this->escape($auth['company_id'])}
    //                                AND n.is_active={$this->escape(1)}
    //                                GROUP BY n.id DESC";
    //                                
    //                                
    //        $getNoti .= "<br><br>";
    //        
    //        
    //    }return $getNoti;
    //}
    
    //public function noti(){
    //    
    //    $auth = $this->getAuth('current_user');
    //    $ret = "";
    //    $noti_array = array();
    //    $noti_name = array();
    //    $noti_get_tbl = $this->query("SELECT *
    //                                FROM tbnotification
    //                                WHERE userID={$this->escape($auth['id'])}
    //                                AND is_active={$this->escape(1)}
    //                                ORDER BY date DESC","array");
    //    
    //        
    //    foreach($noti_get_tbl as $noti){
    //        array_push($noti_name,$noti_get_tbl);
    //        
    //        
    //        // For Mention
    //        if($noti['type']=="1"){ // Mention Post
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //        }elseif($noti['type']=="6"){ // Mention Reply
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy';
    //            $left_select = ' l.id=n.noti_id';
    //        }
    //        
    //        // For Like
    //        elseif($noti['type']=="3"){
    //            $define = ',tp.id as nID,tp.postedBy as postBy,l.likeType as lType';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.user_id  LEFT JOIN tbpost tp on tp.id=l.post_id';
    //            $left_select = ' l.post_id=n.noti_id';
    //        }
    //        elseif($noti['type']=="4"){
    //            $define = ',tp.id as nID,tp.postedBy as postBy,l.likeType as lType';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.user_id  LEFT JOIN tbpost tp on tp.id=l.post_id';
    //            $left_select = ' l.post_id=n.noti_id';
    //        }
    //        
    //        // For Comment
    //        elseif($noti['type']=="2"){
    //            $define = ',l.id as nID,l.postedBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.postedBy';
    //            $left_select = ' l.id=n.noti_id';
    //        }
    //        
    //        // 
    //        // For Task
    //        elseif($noti['type']=="10"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }elseif($noti['type']=="11"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //           
    //        }elseif($noti['type']=="12"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }
    //        // For Task Out of Date
    //        elseif($noti['type']=="13"){
    //            $define = ',l.id as nID,l.createdBy as postBy';
    //            $left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
    //            $left_select = ' l.id=n.noti_id';
    //            
    //        }
    //        
    //        $getNoti = $this->query(" SELECT 
    //                                n.*,
    //                                n.id as notiID,
    //                                u.id as uID,
    //                                us.first_name as uFname,
    //                                us.last_name as uLname,
    //                                n.date as notiDate,
    //                                u.first_name as firstname,
    //                                u.last_name as lastname
    //                                " . $define . "
    //                                FROM tbnotification n
    //                                LEFT JOIN tbuser u on u.id=n.noti_by
    //                                LEFT JOIN " . $noti['table_name'] . " l on " . $left_select . "
    //                                " .  $left_join . "
    //                                WHERE n.userID={$this->escape($auth['id'])}
    //                                AND u.company_id={$this->escape($auth['company_id'])}
    //                                AND n.is_active={$this->escape(1)}
    //                                GROUP BY n.id DESC","array");
    //        
    //        
    //    }
    //        foreach($getNoti as $notification){
    //            if($notification['user_read']==0){
    //                $bg = "background-color: #C0C0C0;";
    //            }else{
    //                $bg = "";
    //            }
    //            
    //            $countLikePost = $this->query("SELECT * FROM tblike WHERE post_id={$this->escape($notification['postID'])} AND likeType={$this->escape(post)}","numrows");
    //            $countLikeComment = $this->query("SELECT * FROM tblike WHERE post_id={$this->escape($notification['postID'])} AND likeType={$this->escape(reply)}","numrows");
    //            $countCommentPost = $this->query("SELECT * FROM tbcomment WHERE post_id={$this->escape($notification['postID'])} AND  commentType={$this->escape(0)} ","numrows");
    //            $countCommentRequest = $this->query("SELECT * FROM tbcomment WHERE post_id={$this->escape($notification['postID'])} AND  commentType={$this->escape(1)} ","numrows");
    //            
    //            array_push($noti_name,$notification['firstname'] . " " . $notification['lastname']);
    //            
    //            $ret = array(   "postID_encrypt"        =>      $this->base_encode_decode("encrypt",$notification['nID']),
    //                            "image"                 =>      $this->avatarPic("tbuser",$notification['noti_by'],"44","44","small","avatar"),
    //                            "name"                  =>      $notification['firstname'] . " " . $notification['lastname'],
    //                            "notiID"                =>      $notification['notiID'],
    //                            "infoID"                =>      $notification['nID'],
    //                            
    //                            "PostedBy"             =>      $notification['uFname'] . " " . $notification['uLname'],
    //                            "notiDate"             =>      $notification['notiDate'],
    //                            "info"                 =>      $notification,
    //                            "notiID_encrypt"       =>      $this->base_encode_decode("encrypt",$notification['notiID']),
    //                            "bg"                   =>      $bg,
    //                            "postedIDBy"           =>      $notification['postBy'],
    //                            "formID"               =>      $notification['formID'],
    //                            "notiUserID"           =>      $notification['noti_by'],
    //                            "authID"               =>      $auth['id'],
    //                            "user_view"            =>      $notification['user_read'],
    //                            "type"                 =>      $notification['type'],
    //                            "likeType"             =>      $type,
    //                            "countLike"            =>      $countLikePost,
    //                            "countLikeReply"       =>      $countLikeComment,
    //                            "countCommentPost"     =>      $countCommentPost,
    //                            "countCommentRequest"  =>      $countCommentRequest,
    //                            "noti_name"            =>      implode(",",$noti_name));
    //            
    //            array_push($noti_array,$ret);
    //        }
    //            
    //    $arr[] = array("notification"=>$noti_array);
    //    
    //    
    //    return $arr;
    //
    //}
    
    
    
    public function noti(){
        
        $auth = $this->getAuth('current_user');
        $ret = "";
        $noti_name = array();
        $noti_get_tbl = $this->query("SELECT *
                                    FROM tbnotification
                                    WHERE userID={$this->escape($auth['id'])}
                                    AND is_active={$this->escape(1)}
                                    ORDER BY date","array");
    //ORDER BY date DESC
            
        foreach($noti_get_tbl as $noti){
            //array_push($noti_name,$noti['table_name']);
            
            // Query to get the post
            if($noti['table_name'] == TBL_POST){
                
                $query = $this->query("SELECT *,N.id as nID,
                                        N.date as notiDate,
                                        LJ.id as LJID,
                                        LJ.postedBy as postBy,
                                        US.first_name as uFname,
                                        US.last_name as uLname,
                                        U.first_name as Fname,
                                        U.last_name as Lname
                                        FROM tbnotification N
                                        LEFT JOIN " . $noti['table_name'] . " LJ on LJ.id=N.noti_id
                                        LEFT JOIN tbuser U on U.id=N.noti_by
                                        LEFT JOIN tbuser US on US.id=LJ.postedBy
                                        WHERE N.is_active={$this->escape(1)}
                                        AND N.userID={$this->escape($auth['id'])}
                                        AND U.company_id={$this->escape($auth['company_id'])}
                                        AND N.table_name={$this->escape($noti['table_name'])}
                                           ","array");
                
                
                $tb_post = "";
                foreach($query as $key => $data){
                    $countCommentPost = $this->query("SELECT * FROM tbcomment WHERE post_id={$this->escape($data['noti_id'])} AND  commentType={$this->escape(0)} ","numrows");
                
                    if($data['user_read']==0){
                        $bg = "background-color: #C0C0C0;";
                    }else{
                        $bg = "";
                    }
                        $tb_post_1 = $data;
                        $tb_post_2 = array("notiID"                =>      $data['nID'],
                                            "notiID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['nID']),
                                           "postID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['noti_id']),
                                           "image"                  =>      $this->avatarPic("tbuser",$data['noti_by'],"44","44","small","avatar"),
                                           "name"                   =>      $data['Fname'] . " " . $data['Lname'],
                                           "countCommentPost"       =>      $countCommentPost,
                                           "postedIDBy"             =>      $data['postBy'],
                                           "PostedBy"               =>      $data['uFname'] . " " . $data['uLname'],
                                           "authID"                 =>      $auth['id'],
                                           "notiDate"               =>      $data['notiDate'],
                                           "notiUserID"             =>      $data['noti_by'],
                                           "auth_level_id"          =>      $auth['user_level_id'],
                                           "bg"                     =>      $bg);
                        $ret = array_merge($tb_post_1,$tb_post_2);
                        $tb_post[] = $ret;
                }
            
            // Query to get the like  
            }elseif($noti['table_name'] == TBL_LIKE){
                
                $query = $this->query("SELECT *,N.id as nID,
                                        N.date as notiDate,
                                        LJ.id as LJID,
                                        TP.postedBy as postBy,
                                        U.first_name as Fname,
                                        U.last_name as Lname
                                        FROM tbnotification N
                                        LEFT JOIN " . $noti['table_name'] . " LJ on LJ.id=N.noti_id
                                        LEFT JOIN tbuser U on U.id=N.noti_by
                                        LEFT JOIN tbuser US on US.id=LJ.user_id
                                        LEFT JOIN tbpost TP on TP.id=LJ.post_id
                                        WHERE N.is_active={$this->escape(1)}
                                        AND N.userID={$this->escape($auth['id'])}
                                        AND U.company_id={$this->escape($auth['company_id'])}
                                        AND N.table_name={$this->escape($noti['table_name'])}
                                           ","array");
                $tb_like = "";
                foreach($query as $data){
                    if($data['user_read']==0){
                        $bg = "background-color:#fafafa";
                    }else{
                        $bg = "";
                    }
                        $tb_like_1 = $data;
                        $tb_like_2 = array("notiID"                =>      $data['nID'],
                                            "notiID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['nID']),
                                           "postID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['noti_id']),
                                           "image"                  =>      $this->avatarPic("tbuser",$data['noti_by'],"44","44","small","avatar"),
                                           "name"                   =>      $data['Fname'] . " " . $data['Lname'],
                                           "countCommentPost"       =>      $countCommentPost,
                                           "postedIDBy"             =>      $data['postBy'],
                                           "authID"                 =>      $auth['id'],
                                           "notiDate"               =>      $data['notiDate'],
                                           "notiUserID"             =>      $data['noti_by'],
                                           "auth_level_id"          =>      $auth['user_level_id'],
                                           "bg"                     =>      $bg);
                        $ret =  array_merge($tb_like_1,$tb_like_2);
                        $tb_like[] = $ret;
                }
            // Query to get the comment     
            }elseif($noti['table_name'] == TBL_COMMENT){
                $query = $this->query("SELECT *,N.id as nID, LJ.id as LJID FROM tbnotification N
                                            LEFT JOIN " . $noti['table_name'] . " LJ on LJ.id=N.noti_id
                                            LEFT JOIN tbuser U on U.id=N.noti_by
                                            WHERE N.is_active={$this->escape(1)}
                                            AND N.userID={$this->escape($auth['id'])}
                                            AND U.company_id={$this->escape($auth['company_id'])}
                                            AND N.table_name={$this->escape($noti['table_name'])}
                                           ","array");
                $tb_comment = "";
                foreach($query as $data){
                    if($data['user_read']==0){
                        $bg = "background-color:#fafafa";
                    }else{
                        $bg = "";
                    }
                        $tb_comment_1 = $data;
                        $tb_comment_2 = array(  "notiID"                =>      $request['nID'],
                                                "notiID_encrypt"            =>      $this->base_encode_decode("encrypt",$data['nID']),
                                                "auth_level_id"          =>      $auth['user_level_id'],
                                                "bg"                        =>        $bg);
                        $ret = array_merge($tb_comment_1,$tb_comment_2);
                        $tb_comment[] = $ret;
                }
                
            // Query to get the tasks     
            }elseif($noti['table_name'] == TBL_TASKS){
                //$define = ',l.id as nID,l.createdBy as postBy';
                //$left_join = 'LEFT JOIN tbuser us on us.id=l.createdBy LEFT JOIN tbuser mu on mu.id=n.userID';
                //$left_select = ' l.id=n.noti_id';
                $query = $this->query("SELECT *,N.id as nID,
                                        N.date as notiDate,
                                        LJ.id as LJID,
                                        LJ.createdBy as postBy,
                                        US.first_name as uFname,
                                        US.last_name as uLname,
                                        U.first_name as Fname,
                                        U.last_name as Lname
                                        FROM tbnotification N
                                        LEFT JOIN " . $noti['table_name'] . " LJ on LJ.id=N.noti_id
                                        LEFT JOIN tbuser U on U.id=N.noti_by
                                        LEFT JOIN tbuser US on us.id=LJ.createdBy
                                        LEFT JOIN tbuser MU on MU.id=N.userID
                                        WHERE N.is_active={$this->escape(1)}
                                        AND N.userID={$this->escape($auth['id'])}
                                        AND U.company_id={$this->escape($auth['company_id'])}
                                        AND N.table_name={$this->escape($noti['table_name'])}
                                           ","array");
                $tb_tasks = "";
                foreach($query as $data){
                    if($data['user_read']==0){
                        $bg = "background-color:#fafafa";
                    }else{
                        $bg = "";
                    }
                        $tb_tasks_1 = $data;
                        $tb_tasks_2 = array("notiID"                =>      $data['nID'],
                                            "notiID_encrypt"        =>      $this->base_encode_decode("encrypt",$data['nID']),
                                           "postID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['noti_id']),
                                           "image"                  =>      $this->avatarPic("tbuser",$data['noti_by'],"44","44","small","avatar"),
                                           "name"                   =>      $data['Fname'] . " " . $data['Lname'],
                                           "countCommentPost"       =>      $countCommentPost,
                                           "postedIDBy"             =>      $data['postBy'],
                                           "PostedBy"               =>      $data['uFname'] . " " . $data['uLname'],
                                           "authID"                 =>      $auth['id'],
                                           "infoID"                 =>      $data['noti_id'],
                                           "notiDate"               =>      $data['notiDate'],
                                           "auth_level_id"          =>      $auth['user_level_id'],
                                           "notiUserID"             =>      $data['noti_by'],
                                           "bg"                     =>      $bg);
                        $ret = array_merge($tb_tasks_1,$tb_tasks_2);
                        $tb_tasks[] = $ret;
                }
            }else{
               // Request
                $query = $this->query("SELECT *,N.id as nID,
                                        N.date as notiDate,
                                        LJ.id as LJID,
                                        W.id as formID,LJ.Requestor as requestID,
                                        U.first_name as Fname,
                                        U.last_name as Lname,
                                        R.display_name as Requestor_name
                                        FROM tbnotification N
                                        LEFT JOIN " . $noti['table_name'] . " LJ on LJ.id=N.noti_id
                                        LEFT JOIN tbuser U on U.id=N.noti_by
                                        LEFT JOIN tb_workspace W on W.form_table_name=N.table_name
                                        LEFT JOIN tbuser R on R.id=LJ.requestor
                                        WHERE N.is_active={$this->escape(1)}
                                        AND N.userID={$this->escape($auth['id'])}
                                        AND U.company_id={$this->escape($auth['company_id'])}
                                        AND N.table_name={$this->escape($noti['table_name'])}
                                           ","array");
                $tb_request = "";
                foreach($query as $data){
                    if($data['user_read']==0){
                        $bg = "background-color:#fafafa";
                    }else{
                        $bg = "";
                    }
                        $tb_request_1 = $data;
                        $tb_request_2 = array(  "notiID"                =>      $data['nID'],
                                                "notiID_encrypt"        =>      $this->base_encode_decode("encrypt",$data['nID']),
                                                "postID_encrypt"         =>      $this->base_encode_decode("encrypt",$data['noti_id']),
                                                "image"                  =>      $this->avatarPic("tbuser",$data['noti_by'],"44","44","small","avatar"),
                                                "name"                   =>      $data['Fname'] . " " . $data['Lname'],
                                                "countCommentPost"       =>      $countCommentPost,
                                                "formID"                 =>      $data['formID'],
                                                "requestID"              =>      $data['LJID'],
                                                //"postedIDBy"             =>      $data['postBy'],
                                                //"PostedBy"               =>      $data['uFname'] . " " . $data['uLname'],
                                                "authID"                 =>      $auth['id'],
                                                "notiDate"               =>      $data['notiDate'],
                                                "notiUserID"             =>      $data['noti_by'],
                                                "auth_level_id"          =>      $auth['user_level_id'],
                                                "bg"                     =>      $bg);
                        $ret = array_merge($tb_request_1,$tb_request_2);
                        $tb_request[] = $ret;
                }
            }
            
             
        }
             $noti = array("like"       =>        $tb_like,
                          "post"        =>         $tb_post,
                          "comment"     =>         $tb_comment,
                          "tasks"       =>         $tb_tasks,
                          "request"     =>         $tb_request);  
            $arr[] = array("notification"=>$noti);
        
        
        return $arr;
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public function countNotification(){
        $auth = $this->getAuth('current_user');
        $ret = "";
        $noti = $this->query("SELECT *,n.id as notiID, u.id as uID, p.id as postID, n.date as notiDate  FROM tbnotification n
                         LEFT JOIN tbuser u on u.id=n.noti_by
                         LEFT JOIN tbpost p on p.id=n.noti_id
                         WHERE n.userID={$this->escape($auth['id'])} AND n.is_active={$this->escape(1)}
                         AND n.user_read={$this->escape(0)}
                         ","numrows");
        $get_numrows = $this->query("SELECT * FROM `tbtasks` WHERE FIND_IN_SET(" . $auth['id'] . ",member) > 0 AND due_date < NOW()","numrows");
        Tasks::task_validation($auth);
        return $noti + $get_numrows;
    }
    
    /*
     * @type (2) - for comment,
     *
     *
     *
     */
    public function inserNotification($Inserttype,$tbname,$auth,$f_noti,$likeType){
        
        $date = functions::currentDateTime();
        $noti = array();
        $email = array();
        if($Inserttype=="post"){
            $tbname = $tbname;
            $type = "1";
            $explode_user = explode(",",$likeType);
            foreach($explode_user as $user){
                array_push($noti,$user);
            }
        }else if($Inserttype=="mentioncomment"){
            $tbname = $tbname;
            $type = "6";
            $explode_user = explode(",",$likeType);
            foreach($explode_user as $user){
                array_push($noti,$user);
            }
            
        }else if($Inserttype=="comment"){
            $tbname = $tbname;
            $type = "2";
            $get = $this->query("SELECT *, c.postedBy as commentBy, p.postedBy as postBy FROM tbpost p
                                    LEFT JOIN tbcomment c on c.post_id=p.id
                                    LEFT JOIN tbuser u on u.id = c.postedBy
                                    WHERE p.id={$this->escape($f_noti['postID'])} AND p.is_active={$this->escape(1)}","array");
         
            foreach($get as $notiBy){
                
                if($notiBy['commentBy']!=$auth['id']){
                    array_push($noti,$notiBy['commentBy']);
                    array_push($noti,$notiBy['postBy']);
                    
                    //
                        //$json_decode = Settings::noti_settings($notiBy['commentBy'],'notifications');
                        //$json_notifications = $json_decode[0]['notifications'];
                        //$json_comment = $json_notifications['Comment'];
                        //
                        //if($json_comment[1]=="1"){
                        //    $arr[$notiBy['id']] = $notiBy;
                        //}
                }
            }
                $n = implode(",",$noti);
                $arr = $this->query("SELECT * FROM tbuser WHERE id IN ($n) AND is_active='1'","array");
                if($likeType!=""){
                    $msg = "commented and tagged you in a post";
                }else{
                    $msg = "commented on post";
                }
            // Comment Email Notification
            $ret = "";
            foreach($arr as $value){
                $json_decode = Settings::noti_settings($value['id'],'notifications');
                $json_notifications = $json_decode[0]['notifications'];
                $json_comment = $json_notifications['Comment'];
                
                if($json_comment[0]=="1"){
                    $mail = new Mail_Notification();
                    $img = $this->avatarPic("tbuser",$auth['id'],"60","60","small","avatar",'style="float: left;border-radius:50px;margin-right: 5px;"');
                    $name = $auth['first_name'] . " " . $auth['last_name'];
                    $f_name = $auth['first_name'];
                    $mail->set_notifications(array($value['email']=>$value['display_name']),$f_noti['comment'],$img,$auth['id'],$name,$msg,$f_name,"comment");
                }
            }
        }else if($Inserttype=="like"){
            //$tbname = $tbname;
            if($likeType=="post"){
                $tname = "tbpost";
                $type = "3";
                $msg = "like on your post";
                $m = "post";
                $p = 'post';
                $t = 'post';
            }else{
                $tname = "tbcomment";
                $type = "4";
                $msg = "like on your comment";
                $m = "comment";
                $p = 'comment';
                $t = 'reply';
            }
            
            $notiBy = $this->query("SELECT *, l.post_id as LJPID
                                FROM tblike l
                                LEFT JOIN " . $tname . "  t on t.id=l.post_id
                                WHERE t.id={$this->escape($f_noti['searchID'])} AND l.likeType={$this->escape($t)}","row");

            //foreach($get as $notiBy){
                array_push($noti,$notiBy['postedBy']);
                $json_decode = Settings::noti_settings($notiBy['postedBy'],'notifications');
                $json_notifications = $json_decode[0]['notifications'];
                $json_n = $json_notifications['Like'];
                
                if($json_n[1]=="1"){    
                    
                    $path = $this->curPageURL('port') . "/index?type=go&redirect=post?view_type=view_post|=%=|postID=" . $this->base_encode_decode("encrypt",$notiBy['LJPID']);
                    $personDoc = new Person($this,$notiBy['postedBy']);
                    
                    $mail = new Mail_Notification();
                    $img = $this->avatarPic("tbuser",$auth['id'],"60","60","small","avatar",'style="float: left;border-radius:50px;margin-right: 5px;"');
                    $name = $auth['first_name'] . " " . $auth['last_name'];
                    $f_name = $auth['first_name'];
                    $mail->set_notifications(array($personDoc->email=>$personDoc->display_name),$notiBy[$p],$img,$auth['id'],$name,$msg,$f_name,$m,"like",$path);   
                }
            //}
        }
     
            $n = implode(",",array_unique($noti));
            $explode_noti = explode(",",$n);
            foreach($explode_noti as $insert_noti){
                $insert = array("noti_id"	=>	$f_noti['postID'],
                                "type"	        =>	$type,
                                "userID"	=>	$insert_noti,
                                "user_read"	=>	0,
                                "noti_by"	=>	$auth['id'],
                                "date"	        =>	$date,
                                "table_name"    =>      $tbname,
                                "is_active"	=>	1);
                
                if($insert_noti!=0 && $insert_noti!=$auth['id']){
                    $this->insert("tbnotification",$insert);
                    return array("notification_recipients" => $n);
                }
            }
            
    }
    
    public function taskNotification($Inserttype,$tbname,$auth,$id,$t_member,$t_viewer,$t_editor){
        $date = functions::currentDateTime();
        $m_noti = array();
        $v_noti = array();
        $e_noti = array();    
        // For Task
                
        if($Inserttype==""){       
        // Members
        
            $explode_member = explode(",",$t_member);
            foreach($explode_member as $user){
                array_push($m_noti,$user);
            }
        
         
            
            $n = implode(",",array_unique($m_noti));
            $explode_noti = explode(",",$n);
            foreach($explode_noti as $insert_noti){
                $insert = array("noti_id"	=>	$id,
                                "type"	        =>	"10",
                                "userID"	=>	$insert_noti,
                                "user_read"	=>	0,
                                "noti_by"	=>	$auth['id'],
                                "date"	        =>	$date,
                                "table_name"    =>      $tbname,
                                "is_active"	=>	1);
                
                if($insert_noti!=0 && $insert_noti!=$auth['id']){
                    $this->insert("tbnotification",$insert);
                }
                
            }
            
        // Viewers
        
            $explode_viewer = explode(",",$t_viewer);
            foreach($explode_viewer as $user){
                array_push($v_noti,$user);
            }
            
        
        
            $n = implode(",",array_unique($v_noti));
            $explode_noti = explode(",",$n);
            foreach($explode_noti as $insert_noti){
                $insert = array("noti_id"	=>	$id,
                                "type"	        =>	"11",
                                "userID"	=>	$insert_noti,
                                "user_read"	=>	0,
                                "noti_by"	=>	$auth['id'],
                                "date"	        =>	$date,
                                "table_name"    =>      $tbname,
                                "is_active"	=>	1);
                
                if($insert_noti!=0 && $insert_noti!=$auth['id']){
                    $this->insert("tbnotification",$insert);
                }
                
            }
            
        // Editor
        
            $explode_editor = explode(",",$t_editor);
            foreach($explode_editor as $user){
                array_push($e_noti,$user);
            }
        
            $n = implode(",",array_unique($e_noti));
            $explode_noti = explode(",",$n);
            foreach($explode_noti as $insert_noti){
                $insert = array("noti_id"	=>	$id,
                                "type"	        =>	"12",
                                "userID"	=>	$insert_noti,
                                "user_read"	=>	0,
                                "noti_by"	=>	$auth['id'],
                                "date"	        =>	$date,
                                "table_name"    =>      $tbname,
                                "is_active"	=>	1);
                
                if($insert_noti!=0 && $insert_noti!=$auth['id']){
                    $this->insert("tbnotification",$insert);
                }
                
            }
        }elseif($Inserttype=="task_out"){
            // Task Out of Date
            
            $explode_member = explode(",",$t_member);
            foreach($explode_member as $user){
                array_push($m_noti,$user);
            }
        
         
            
            $n = implode(",",array_unique($m_noti));
            $explode_noti = explode(",",$n);
            foreach($explode_noti as $insert_noti){
                $insert = array("noti_id"	=>	$id,
                                "type"	        =>	"13",
                                "userID"	=>	$insert_noti,
                                "user_read"	=>	0,
                                "noti_by"	=>	$auth['id'],
                                "date"	        =>	$date,
                                "table_name"    =>      $tbname,
                                "is_active"	=>	1);
                
                if($insert_noti!=0 && $insert_noti!=$auth['id']){
                    $this->insert("tbnotification",$insert);
                }
                
            }
        }
    }
    
    
    public function insertNofiRequest($requestID,$requestorID,$tbname,$processor){
         $date = functions::currentDateTime();
        $type = "5";
        $insert = array("noti_id"	=>	$requestID,
                            "type"	=>	$type,
                            "userID"	=>	$processor,
                            "user_read"	=>	0,
                            "noti_by"	=>	$requestorID,
                            "date"	=>	$date,
                            "table_name"=>      $tbname,
                            "is_active"	=>	1);
            
            
                $this->insert("tbnotification",$insert);
    }
    public function last($array, $key) {
        end($array);
        return $key === key($array);
    }

}

?>